﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    [Serializable]
    public sealed class CheckTexture
    {

        private enum ReadableMode
        {

            [EnumLabel("Not Check")]
            NotCheck,
            [EnumLabel("Force Readable")]
            ForceReadable,
            [EnumLabel("Force Not Readable")]
            ForceNotReadable
        }

        [SerializeField]
        [EnumLabel]
        [Tooltip("The readable check mode.")]
        private ReadableMode readableMode;

        private enum AlphaMode
        {
            [EnumLabel("Not Check")]
            NotCheck,
            [EnumLabel("Check Alpha Is Transparcy")]
            Transparcy
        }

        [Tooltip("Check the alpha check mode.")]
        [EnumLabel]
        [SerializeField]
        private AlphaMode alphaMode;

        [SerializeField]
        [Tooltip("The max texture size of no larger than this, 0 means no check.")]
        private int maxTextureSize;

        private enum FormatMode
        {
            [EnumLabel("Not Check")]
            NotCheck,
            [EnumLabel("Force Compressed")]
            ForceCompressed,
            [EnumLabel("Force Not Compressed")]
            ForceNotCompressed
        }

        [Tooltip("The format check mode.")]
        [EnumLabel]
        [SerializeField]
        private FormatMode formatMode;


        private enum MipmapMode
        {
            [EnumLabel("Not Check")]
            NotCheck,
            [EnumLabel("Force Enable Mipmap")]
            ForceEnableMipmap,
            [EnumLabel("Force Disable Mipmap")]
            ForceDisableMipmap,
            [EnumLabel("Force Disable Sprite & Enable Others")]
            EnableOthers
        }

        [SerializeField]
        [Tooltip("The mipmap check mode.")]
        [EnumLabel]
        private MipmapMode mipmapMode;

        private enum WarpMode
        {
            [EnumLabel("Not Check")]
            NotCheck,
            [EnumLabel("Force Repeat")]
            ForceRepeat,
            [EnumLabel("Force Clamp")]
            ForceClamp
        }

        [Tooltip("The warp check mode.")]
        [SerializeField]
        [EnumLabel]
        private WarpMode warpMode;


        private enum FilterMode
        {
            [EnumLabel("Not Check")]
            NotCheck,
            [EnumLabel("Force Point")]
            ForcePoint,
            [EnumLabel("Force Bilinear")]
            ForceBilinear,
            [EnumLabel("Force Trilinear")]
            ForceTrilinear
        }

        [Tooltip("The filter check mode.")]
        [SerializeField]
        [EnumLabel]
        private FilterMode filterMode;


        public void CheckTextureImporter(AssetCheckReport report,TextureImporter import)
        {
            if (this.readableMode == ReadableMode.ForceReadable)
            {
                if (!import.isReadable)
                {
                    report.ShowWarnInfo("The texture is not readable, but it force readable.");
                }
            }
            else if (this.readableMode == ReadableMode.ForceNotReadable && import.isReadable)
            {
                report.ShowWarnInfo("The texture is readable, but it force not readable.");
            }

            if (this.alphaMode == AlphaMode.Transparcy && import.DoesSourceTextureHaveAlpha() && !import.alphaIsTransparency)
            {
                report.ShowWarnInfo("Alpha is not set as transparency, while this texture has alpha channel.");
            }

            if (this.formatMode == FormatMode.ForceCompressed)
            {
                if (import.textureCompression == TextureImporterCompression.Uncompressed)
                {
                    report.ShowWarnInfo("The texture compression is <b><color=orange>{0}</color></b>, but it force use compressed format.",import.textureCompression);
                }
            }
            else if (this.formatMode == FormatMode.ForceNotCompressed && import.textureCompression == TextureImporterCompression.Compressed)
            {
                report.ShowWarnInfo("The texture compression is <b><color=orange>{0}</color></b>, but it force not use compressed format.",import.textureCompression);
            }

            if (this.mipmapMode == MipmapMode.ForceEnableMipmap)
            {
                if (!import.mipmapEnabled)
                {
                    report.ShowWarnInfo("The mipmap of this texture is not enabled, but it require enabled.");
                }
            }
            else if (this.mipmapMode == MipmapMode.ForceDisableMipmap)
            {
                if (import.mipmapEnabled)
                {
                    report.ShowWarnInfo("The mipmap of this texture is enabled, but it require disabled.");
                }
            }
            else if (this.mipmapMode == MipmapMode.EnableOthers)
            {
                if (import.textureType == TextureImporterType.Sprite)
                {
                    if (import.mipmapEnabled)
                    {
                        report.ShowWarnInfo("The mipmap of this sprite is enabled, but it require disabled.");
                    }
                }
                else if (!import.mipmapEnabled)
                {
                    report.ShowWarnInfo("The mipmap of this texture is not enabled, but it require enabled.");
                }
            }

            if (this.warpMode == WarpMode.ForceClamp)
            {
                if (import.wrapMode != TextureWrapMode.Clamp)
                {
                    report.ShowWarnInfo("The texture warp in <b><color=orange>{0}</color></b>, but it require in Clamp mode.",import.wrapMode);
                }
            }
            else if (this.warpMode == WarpMode.ForceRepeat && import.wrapMode !=  TextureWrapMode.Repeat)
            {
                report.ShowWarnInfo("The texture warp in <b><color=orange>{0}</color></b>, but it require in Repeat mode.",import.wrapMode);
            }

            if (this.filterMode == FilterMode.ForcePoint)
            {
                if (import.filterMode != UnityEngine.FilterMode.Point)
                {
                    report.ShowWarnInfo("The texture filter is <b><color=orange>{0}</color></b>, but it require use Point mode.",import.filterMode);
                }
            }
            else if (this.filterMode == FilterMode.ForceBilinear)
            {
                if (import.filterMode != UnityEngine.FilterMode.Bilinear)
                {
                    report.ShowWarnInfo("The texture filter is <b><color=orange>{0}</color></b>, but it require use Bilinear mode.",import.filterMode);
                }
            }
            else if (this.filterMode == FilterMode.ForceTrilinear && import.filterMode != UnityEngine.FilterMode.Trilinear)
            {
                report.ShowWarnInfo("The texture filter is <b><color=orange>{0}</color></b>, but it require use Trilinear mode.",import.filterMode);
            }
        }

        public void CheckTextureMaxSize(AssetCheckReport report,Texture texture)
        {
            if (this.maxTextureSize > 0 && (texture.width > this.maxTextureSize || texture.height > this.maxTextureSize))
            {
                report.ShowWarnInfo("The texture size is: <b><color=orange>{0}x{1}</color></b>, larger than the rule size: <b><color=orange>{2}</color></b>.",
                    texture.width,
                    texture.height,
                    this.maxTextureSize
                );
            }
        }

    }
}

